package org.dromara.system.job;

import com.aizuda.snailjob.client.job.core.annotation.JobExecutor;
import com.aizuda.snailjob.client.job.core.dto.JobArgs;
import com.aizuda.snailjob.client.model.ExecuteResult;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.dromara.system.domain.SysSystem;
import org.dromara.system.service.ISysSystemService;
import org.dromara.system.utils.DataStatusUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.List;

/**
 * 江心洲检查程序(5桥)
 */
@JobExecutor(name = "CheckFiveBridgeStatusJob")
@Slf4j
@Component
@RequiredArgsConstructor(onConstructor_ = @Autowired)
public class CheckFiveBridgeStatusJob {

    private final ISysSystemService sysSystemService;

    public ExecuteResult jobExecute(JobArgs jobArgs) {
        log.info("检查江心洲服务状态");

        List<SysSystem> fiveDataSource = sysSystemService.getFiveDataSource();
        List<SysSystem> updatedSystems = new ArrayList<>();

        for (SysSystem sysSystem : fiveDataSource) {
            SysSystem info = DataStatusUtils.getInfo(sysSystem.getIp());
            sysSystem.setStatus(info.getStatus());
            sysSystem.setUpdateTime(info.getUpdateTime());
            updatedSystems.add(sysSystem);
        }

        if (!updatedSystems.isEmpty()) {
            sysSystemService.updateBatchById(updatedSystems);
            log.info("更新系统状态成功，共更新 {} 条记录", updatedSystems.size());
        } else {
            log.info("没有需要更新的系统状态");
        }
        return ExecuteResult.success("检查江心洲服务状态成功");
    }


}
